package com.zryc.service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class OrderServiceImpl3 {
    int n=0;
    //设置当前message 方法作为一个sentinel资源
    @SentinelResource(value = "message",fallback = "handle",fallbackClass = OrderServiceImpl3ExceptionHandle.class,
    blockHandlerClass = OrderServiceImpl3ExceptionHandle.class )
    public String massage(){
        synchronized (this){
            n++;
        }
        if (n%3!=0){
            throw new RuntimeException("发生了一次异常");
        }
//        if (n%2 == 0){
//            try {
//                Thread.sleep(300);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
        log.info("Message! Go ! Go!");
        return "Test Message";
    }

    public String handle(Throwable throwable){
        log.error("{}",throwable);
        return "程序出错了";
    }
    public String handleBlock(BlockException blockException){
        log.error("{}",blockException);
        if (blockException instanceof FlowException){
            return "服务被限流了";
        }
        if (blockException instanceof DegradeException){
            return "服务被降级了";
        }
        return "服务被限制访问了";

    }
}
